<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>队列的封装</title>

</head>
<body>
<script>

    //封装队列
    function Queue() {
        //定义一个数组
        this.items = [];
        //添加元素
        Queue.prototype.enqueue = function (element) {
           this.items.push(element);
        };
        //删除元素
        Queue.prototype.dequeue=function () {
            return this.items.shift();
        };
        //查看队首元素
        Queue.prototype.front = function () {
            return this.items[0];
        };
        //查看队列是否为空
        Queue.prototype.isEmpty = function () {
          return this.items.length == 0;
        };
        //查看元素个数
        Queue.prototype.size = function () {
            return this.items.length;
        };
        //toString方法
        Queue.prototype.toString = function () {
            var resultStr = '';
            for (let i = 0; i < this.items.length; i++) {
                resultStr += this.items[i].element +'';
            }
            return resultStr;
        };
    }
    //创建一个对象
    var queue = new Queue();

    var x=1;
    var y = 2;
    console.log(queue.enqueue(x));
    console.log(queue.enqueue(y));
    // console.log(queue.dequeue());
    console.log(queue.front());
    console.log(queue.size());

</script>
</body>
</html>